.globl userret
userret:
    mv t0, a0          # t0 = &trapframe

    # 初始化寄存器（a0~a7, sp, ra, s0~s11, t0~t6等）
    ld ra, 0(a0)      # ra = trapframe[0]
    ld sp, 8(a0)      # sp = trapframe[1]
    ld gp, 16(a0)     # gp = trapframe[2]
    ld tp, 24(a0)     # tp = trapframe[3]
    ld t0, 32(a0)     # t0 = trapframe[4]
    ld t1, 40(a0)     # t1 = trapframe[5]
    ld t2, 48(a0)     # t2 = trapframe[6]
    ld s0, 56(a0)     # s0 = trapframe[7]
    ld s1, 64(a0)     # s1 = trapframe[8]
    
    ld a1, 80(a0)     # a1 = trapframe[10]
    ld a2, 88(a0)     # a2 = trapframe[11]
    ld a3, 96(a0)     # a3 = trapframe[12]
    ld a4, 104(a0)    # a4 = trapframe[13]
    ld a5, 112(a0)    # a5 = trapframe[14]
    ld a6, 120(a0)    # a6 = trapframe[15]
    ld a7, 128(a0)    # a7 = trapframe[16]
    ld s2, 136(a0)    # s2 = trapframe[17]
    ld s3, 144(a0)    # s3 = trapframe[18]
    ld s4, 152(a0)    # s4 = trapframe[19]
    ld s5, 160(a0)    # s5 = trapframe[20]
    ld s6, 168(a0)    # s6 = trapframe[21]
    ld s7, 176(a0)    # s7 = trapframe[22]
    ld s8, 184(a0)    # s8 = trapframe[23]
    ld s9, 192(a0)    # s9 = trapframe[24]
    ld s10, 200(a0)   # s10 = trapframe[25]
    ld s11, 208(a0)   # s11 = trapframe[26]

    ld t3, 216(a0)    # t3 = trapframe[27]
    ld t4, 224(a0)    # t4 = trapframe[28]
    ld t5, 232(a0)    # t5 = trapframe[29]
    ld t6, 240(a0)       # 恢复 t6
    ld t1, 256(a0)       # 恢复 sstatus
    csrw sstatus, t1
    # 恢复用户态的程序计数器（epc）
    ld t0, 248(a0)      # 获取 epc
    csrw sepc, t0       # 将 epc 恢复到 sepc 寄存器
    
    ld a0, 72(a0)     # a0 = trapframe[9]
    sret
